package day26;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Date;

public class Demo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //1.获取类对象
        Class c = Class.forName("day26.Person");
        //2.获取单个属性
        Field f1 = c.getDeclaredField("name");
        Field f2 = c.getDeclaredField("time");
        //3.设置属性
        Person p = new Person();
        //设置私有权限  默认false不开启
        f1.setAccessible(true);
        f1.set(p,"张三");
        f2.set(p,new Date());
        System.out.println(p.getName()+"  "+p.getTime());
        //4.通过反射获取属性值
        Object value = f1.get(p);
        System.out.println("反射获取属性值"+value);
    }
}
